﻿using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.mes.md;
using ZR.Model.MES.op.ZR.Model.mes.md;
using ZR.Service.MES.md.IService;
using ZR.Service;
using ZR.Service.MES.op.IService;
using ZR.Model.MES.op.DTO;
using Microsoft.AspNetCore.Http.HttpResults;
using Org.BouncyCastle.Asn1.Esf;

namespace ZR.Service.MES.op
{
    [AppService(ServiceType = typeof(IOperationService), ServiceLifetime = LifeTime.Transient)]
    public class OperationService : BaseService<OpStatistics>, IOperationService
    {
        public List<OpStatisticsDTO> GetAllData(string workshopID)
        {
            decimal myDecimal = 123.456789m;
            string formattedDecimal = myDecimal.ToString("0.00"); // 保留两位小数

            List<OpStatisticsDTO> OpStatisticsDTOList= new List<OpStatisticsDTO>();
            List<OpStatistics>  OpStatisticsList =Queryable().Where(it => it.WorkshopId == workshopID ).ToList();
            foreach (var OpStatistics in OpStatisticsList)
            {
                OpStatisticsDTO opStatisticsDTO=new OpStatisticsDTO();
                opStatisticsDTO.WorkshopId = OpStatistics.WorkshopId;
                opStatisticsDTO.WorkshopName = OpStatistics.WorkshopName;
                opStatisticsDTO.ProductDate = OpStatistics.ProductDate;
                opStatisticsDTO.PlanNum = OpStatistics.PlanNum;
                opStatisticsDTO.ProductedNum = OpStatistics.ProductedNum;
                decimal num1 = (decimal) OpStatistics.ProductedNum / (decimal)OpStatistics.PlanNum*100;
                opStatisticsDTO.ProductProgressRate = (int)num1 ;
                opStatisticsDTO.GoodproductsNum = OpStatistics.GoodproductsNum;
                opStatisticsDTO.DefectiveProductsNum = OpStatistics.DefectiveProductsNum;
                decimal num2 = (decimal)OpStatistics.DefectiveProductsNum / (decimal)(OpStatistics.GoodproductsNum + OpStatistics.DefectiveProductsNum);
                opStatisticsDTO.QualityRate = num2.ToString("f2");
                opStatisticsDTO.ProductStarttime = OpStatistics.ProductStarttime;
                opStatisticsDTO.ProductEndtime = OpStatistics.ProductEndtime;
                if(DateTime.Now> opStatisticsDTO.ProductStarttime&& DateTime.Now < opStatisticsDTO.ProductEndtime)
                {
                    opStatisticsDTO.isProducting = true;

                }
                else
                {
                    opStatisticsDTO.isProducting = false;
                }

                opStatisticsDTO.numSort = OpStatisticsList.IndexOf(OpStatistics);

                opStatisticsDTO.LineId = OpStatistics.LineId;
                opStatisticsDTO.LineName = OpStatistics.LineName;
                opStatisticsDTO.ProductEndtime = OpStatistics.ProductEndtime;

                OpStatisticsDTOList.Add(opStatisticsDTO);
            }



            return OpStatisticsDTOList;
        }
    }
}

